<template>
  <div class="component-root">

    <el-card>
      <p slot="header">
        <span><b>关注公众号链接</b></span>
        <el-button-group class="base-el-float-right">
          <el-button type="primary" icon="el-icon-edit" @click="handleAdUpdate('关注公众号链接', 'accountLinkData')">更新</el-button>
        </el-button-group>
      </p>
      <el-input v-model="accountLinkData.data" />
    </el-card>

    <el-card>
      <p slot="header">
        <span><b>资质图片</b></span>
        <el-button-group class="base-el-float-right">
          <el-button type="primary" icon="el-icon-edit" @click="handleAdUpdate('资质图片', 'certImageData')">更新</el-button>
        </el-button-group>
      </p>
      <BatchUpload v-model="certImageData.data" :limit="10" :size="10" />
    </el-card>

    <!--    <el-card>-->
    <!--      <p slot="header">-->
    <!--        <span><b>客服信息</b></span>-->
    <!--        <el-button-group class="base-el-float-right">-->
    <!--          <el-button type="primary" icon="el-icon-edit" @click="handleAdUpdate('客服信息', 'serviceDeskContact')">更新</el-button>-->
    <!--        </el-button-group>-->
    <!--      </p>-->
    <!--      <p>文本描述</p>-->
    <!--      <el-input v-model="serviceDeskContact.message" />-->
    <!--      <p>文本内容</p>-->
    <!--      <el-input v-model="serviceDeskContact.clipboard" />-->
    <!--    </el-card>-->

    <!--    <el-card>-->
    <!--      <p slot="header">-->
    <!--        <span><b>申请说明(打款说明)</b></span>-->
    <!--        <el-button-group class="base-el-float-right">-->
    <!--          <el-button type="primary" icon="el-icon-edit" @click="handleAdUpdate('申请说明(打款说明)', 'applyTip')">更新</el-button>-->
    <!--        </el-button-group>-->
    <!--      </p>-->
    <!--      <el-input v-model="applyTip.message" />-->
    <!--    </el-card>-->

    <!--    <el-card>-->
    <!--      <p slot="header">-->
    <!--        <span><b>入驻管理说明</b></span>-->
    <!--        <el-button-group class="base-el-float-right">-->
    <!--          <el-button type="primary" icon="el-icon-edit" @click="handleAdUpdate('入驻管理说明', 'matchmakerStatusTip')">更新</el-button>-->
    <!--        </el-button-group>-->
    <!--      </p>-->
    <!--      <el-input v-model="matchmakerStatusTip.message" />-->
    <!--    </el-card>-->

  </div>
</template>
<script>
import { insertRecord, queryByCondition, updateById } from '@/api/commoncrud'
import BatchUpload from '@/components/BatchUpload'
export default {
  name: 'OtherConfigContent',
  components: { BatchUpload },
  data() {
    return {
      accountLinkData: {
        data: ''
      },
      certImageData: {
        data: []
      }
    }
  },
  async mounted() {
    await this.fetchData()
  },
  methods: {
    async fetchData() {
      this.$loading({
        text: '数据加载中...'
      })
      try {
        // 关注公众号链接
        const AccountLinkRes = await queryByCondition('globalconfig', 0, 1, { name: 'account_link' })
        let AccountLinkData = AccountLinkRes.data.documentList[0]
        if (!AccountLinkData) {
          const newDataRes = await insertRecord('globalconfig', { name: 'account_link', data: '' })
          AccountLinkData = newDataRes.data
        }
        this.accountLinkData = { _id: AccountLinkData._id, data: AccountLinkData.data }

        // 资质图片
        const CertImageRes = await queryByCondition('globalconfig', 0, 1, { name: 'cert_images' })
        let CertImageData = CertImageRes.data.documentList[0]
        if (!CertImageData) {
          const newDataRes = await insertRecord('globalconfig', { name: 'cert_images', data: [] })
          CertImageData = newDataRes.data
        }
        this.certImageData = { _id: CertImageData._id, data: CertImageData.data }
      } catch (e) {
        this.$message.error('查询失败!')
      }
      this.$loading({}).close()
    },
    async handleAdUpdate(title, key) {
      const configData = { data: this[key].data }
      await this.$alert(`确认更新${title}?`, '消息提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        showCancelButton: true,
        callback: async action => {
          if (action === 'confirm') {
            await updateById('globalconfig', this[key]._id, { ...configData })
            this.$message.success('更新成功!')
          }
          await this.fetchData()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.component-root{
  margin: 20px;
}
.margin-right{
  margin-right: 20px;
}
</style>
